package com.gyc.simple.sysuser.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyc.simple.sysuser.dao.base.IUserMapper;
import com.gyc.simple.sysuser.dao.base.IUserRoleMapper;
import com.gyc.simple.sysuser.entity.base.IUser;
import com.gyc.simple.sysuser.entity.base.IUserRoleKey;
import com.gyc.simple.sysuser.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @program: Inaction
 * @description:
 * @author: Mr.gyc
 * @create: 2019-09-01 17:36
 */
@Transactional
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IUserRoleMapper iUserRoleMapper;

    @Override
    public IUser getUserByLoginName(String lName) {
        return userMapper.selectByName(lName);
    }

    @Override
    public IUser selectByName(String lName) {
        return userMapper.selectByName(lName);
    }

    @Override
    public List<String> batchAddUser(List<IUser> iUsers) {

        List<String> Dname = userMapper.checkName(iUsers);
        if(Dname.size()>0){
            return Dname;
        }else{
            userMapper.batchAddUser(iUsers);
        }
        return Dname;
    }

    @Override
    public PageInfo queryUserListByPageAndCondition(Map paraMap) {
        PageHelper.startPage(Integer.parseInt((String) paraMap.get("pageNum")),Integer.parseInt((String)paraMap.get("pageSize")));
        return new PageInfo(userMapper.queryUserListByPageAndCondition(paraMap));
    }

    @Override
    public int updateByPrimaryKeySelective(IUser iUser) {

        handlerUserRole(iUser);
        //更新用户的基本信息
        return userMapper.updateByPrimaryKeySelective(iUser);
    }

    @Override
    public int insertSelective(IUser iUser) {
        return userMapper.insertSelective(iUser);
    }

    @Override
    public Boolean ifPresentLname(String iuLname) {
        IUser iUser = userMapper.selectByLname(iuLname);
        return Optional.ofNullable(iUser).isPresent();
    }

    @Override
    public Boolean deleteById(String iuId) {
        return userMapper.deleteByPrimaryKey(iuId)==1?true:false;
    }

    @Override
    public int deleteAllByIds(String[] arrayids) {
        return userMapper.deleteAllByIds(arrayids);
    }

    private void handlerUserRole(IUser iUser){
        if(iUser.getList()!=null&&iUser.getList().size()>0){
            List<IUserRoleKey> iUserRoleKeys = new ArrayList<>(iUser.getList().size());
            for(int i=0;i<iUser.getList().size();i++){
                IUserRoleKey iUserRoleKey = new IUserRoleKey();
                iUserRoleKey.setIuId(iUser.getIuId());
                iUserRoleKey.setIroleid((String) iUser.getList().get(i));
                iUserRoleKeys.add(iUserRoleKey);
            }
            //删除用户的用户与角色所有关联
            iUserRoleMapper.deleteByUid(iUser.getIuId());
            //添加用户与角色的新关联关系
            iUserRoleMapper.batchAddUserRole(iUserRoleKeys);
        }
    }
}
